﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using com.google.zxing.oned;
using System.Windows.Media.Imaging;
using com.google.zxing;
using com.google.zxing.common;
using Microsoft.Devices;

namespace ALiteTeam.PackageTracer.Core
{
    /// <summary>
    /// 条码扫描识别类
    /// </summary>
    public class BarCodeIdentifier
    {
        private int _Rotatebs;

        private  MultiFormatOneDReader _Rreader;

        public BarCodeIdentifier()
        {

            _Rreader = new MultiFormatOneDReader(null);
        }

        /// <summary>
        /// 根据摄像头的内容识别其中的条形码
        /// </summary>
        /// <param name="camera"></param>
        /// <returns></returns>
        public string Identify(PhotoCamera camera)
        {
            string result = null;
            try
            {
                var width = Convert.ToInt32(camera.PreviewResolution.Width);
                var height = Convert.ToInt32(camera.PreviewResolution.Height);

                WriteableBitmap wBmp = new WriteableBitmap(width, height);
                var ARGBPx = new int[width * height];

                camera.GetPreviewBufferArgb32(ARGBPx);
                for (int i = 0; i < ARGBPx.Length; i++)
                {
                    ARGBPx[i] = this.ColorToGray(ARGBPx[i]);
                }

                ARGBPx.CopyTo(wBmp.Pixels, 0);
                wBmp.Invalidate();

                RGBLuminanceSource luminiance = new RGBLuminanceSource(wBmp, wBmp.PixelWidth, wBmp.PixelHeight);
                HybridBinarizer binarizer = new HybridBinarizer(luminiance);
                BinaryBitmap binBitmap = new BinaryBitmap(binarizer);

                if ((this._Rotatebs % 3) == 0)
                {
                    binBitmap.rotateCounterClockwise();
                }

                Result resultObject = this._Rreader.decode(binBitmap);

                result = resultObject.Text;
            }
            catch
            {
                this._Rotatebs++;
            }

            return result;
        }

        /// <summary>
        /// 把一个色彩从RGB转换为灰阶
        /// </summary>
        /// <param name="color"></param>
        /// <returns></returns>
        private int ColorToGray(int color)
        {
            int num2 = color >> 0x18;
            int num3 = (color & 0xff0000) >> 0x10;
            int num4 = (color & 0xff00) >> 8;
            int num5 = color & 0xff;
            if ((num3 == num4) && (num4 == num5))
            {
                return color;
            }
            int num6 = ((((7 * num3) + (0x26 * num4)) + (0x13 * num5)) + 0x20) >> 6;
            return (((((num2 & 0xff) << 0x18) | ((num6 & 0xff) << 0x10)) | ((num6 & 0xff) << 8)) | (num6 & 0xff));
        }
    }
}
